import { useNuxtApp } from '#app'


interface NavigateOptions {
    path: string
    query?: Record<string, string>
}

/**
 * 国际化导航
 * @param options  
 * @returns 
 * @example
 * i18nNavigateTo('/search')
 * i18nNavigateTo({
 *     path: '/search',
 *     query: {
 *       page: 1,
 *       sort: 'asc'
 *     }
 * })
 */
export const i18nNavigateTo = (options: string | NavigateOptions) => {
    const { $i18n } = useNuxtApp()
    const pathLocale = $i18n.locale.value

    if (typeof options === 'string') {
        const localizedPath = `/${pathLocale}${options}`
        return navigateTo(localizedPath)
    }

    const { path, query } = options
    const localizedPath = `/${pathLocale}${path}`
    return navigateTo({
        path: localizedPath,
        query
    })
}

/**
 * 国际化路由是否激活
 * @param path 
 * @returns 
 * @example
 * isActiveRoute('/search')
 */
export const isActiveRoute = (path: string) => {
    const { $i18n } = useNuxtApp()
    const route = useRoute()
    return computed(() => route.path === `/${$i18n.locale.value}${path}`)
}

